package com.hm.system.service.mongo;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.hm.common.common.FileUtil;
import com.hm.common.common.FileUtil.UploadFile;
import com.hm.system.auto.SystemProperties;
import com.hm.system.mongo.domain.def.SysFileDef;
import com.hm.system.mongo.domain.system.QSysFileInfo;
import com.hm.system.mongo.domain.system.SysFileInfo;
import com.hm.system.share.repository.mongo.system.SysFileInfoRepository;
import com.hm.system.share.service.mongo.system.SysFileInfoService;

/**
 * @author shishun.wang
 * @date 2016年6月6日 下午9:18:19
 * @version 1.0
 * @describe 
 */
@Service
public class SysFileInfoServiceImpl implements SysFileInfoService {
	
	private static final Logger logger = LoggerFactory.getLogger(SysFileInfoServiceImpl.class);

	@Autowired
	private SysFileInfoRepository sysFileInfoRepository;
	
	@Autowired
	private SystemProperties webAppProperties;
	
	@Override
	public String addSysFileInfo(SysFileInfo sysFileInfo) {
		return sysFileInfoRepository.save(sysFileInfo).getId();
	}

	@Override
	public SysFileInfo getSysFileInfoByHashCode(Integer hashCode) {
		return sysFileInfoRepository.findOne(QSysFileInfo.sysFileInfo.hashCode.eq(hashCode));
	}

	@Override
	public SysFileInfo getSysFileInfoById(String id) {
		return sysFileInfoRepository.findOne(id);
	}

	@Override
	public String upload(MultipartFile file, SysFileDef fileDef) {
		String prefix = webAppProperties.getFileUpload().getTempFilePath() + "/" + fileDef;
		UploadFile uploadFile = null;
		try {
			uploadFile = FileUtil.upload(prefix, new MultipartFile[]{file});

			if (new File(uploadFile.getUploadPath()).length() > webAppProperties.getFileUpload().getFileMaxSize()) {
				FileUtil.deleteFile(uploadFile.getUploadPath());
				throw new Exception("上传文件太大,保存失败");
			}
			int hashCode = FileUtil.hashCode(uploadFile.getUploadPath());

			SysFileInfo fileInfo = this.getSysFileInfoByHashCode(hashCode);
			if (null != fileInfo) {
				// 删除临时文件
				FileUtil.deleteFile(uploadFile.getUploadPath());

				return fileInfo.getId();
			} else {
				String newPath = uploadFile.getUploadPath().replace(webAppProperties.getFileUpload().getTempFilePath(),
						webAppProperties.getFileUpload().getOriginalFilePath());
				
				String temp[] = newPath.split("/");
				FileUtil.exists(newPath.replace(temp[temp.length - 1], ""), true);
				// copy
				FileUtil.copy(new File(uploadFile.getUploadPath()), new File(newPath));

				// 删除临时文件
				FileUtil.deleteFile(uploadFile.getUploadPath());

				SysFileInfo sysFileInfo = new SysFileInfo();
				{
					sysFileInfo.setFileDef(fileDef);
					sysFileInfo.setBaseUrl(newPath);
					sysFileInfo.setFileName(uploadFile.getFileName());
					sysFileInfo.setHashCode(FileUtil.hashCode(newPath));
				}

				return this.addSysFileInfo(sysFileInfo);
			}
		} catch (Exception e) {
			logger.error("文件上传异常:"+e);
			return null;
		}
	}

}
